package com.changfen.controller;

import com.changfen.Result;
import com.changfen.dto.OrderDto;
import com.changfen.feign.OrderClient;
import com.changfen.service.AccountService;
import com.changfen.service.TccAccountService;
import io.seata.rm.tcc.api.BusinessActionContext;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;


/**
 * @author changfeng.chen
 * @date 2021/5/13
 * Email: changfeng.chen@things-matrix.com
 */
@RestController
@RequestMapping("/account")
@Slf4j
public class AccountController {
    @Resource
    private AccountService accountService;

    @Resource
    private TccAccountService tccAccountService;

    @Resource
    private OrderClient orderClient;

    @PutMapping("/createOrder/{userId}")
    public Result<String> createOrder(@PathVariable Long userId) {
        accountService.addAccountAndOrder(userId);
        return Result.ok();
    }

    @PutMapping("/createTccOrder/{userId}")
    @GlobalTransactional
    public Result<String> createTccOrder(@PathVariable Long userId) {
        tccAccountService.tryCreate(null,userId);
        return Result.ok();
    }
}
